package com.ztc.ZtcRouteAttachment.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;
import com.ztc.ZtcRouteAttachment.dao.ZtcRouteAttachmentDao;
@Service("ZtcRouteAttachment")
public class ZtcRouteAttachmentManager{
    @Autowired
    ZtcRouteAttachmentDao ztcRouteAttachmentDao;
    
    @Autowired
    AttachmentManager attachmentManager;
    
    public String save(ZtcRouteAttachment ztcRouteAttachment){
    	return ztcRouteAttachmentDao.save(ztcRouteAttachment);
    }
    
    public List<ZtcRouteAttachment> getAll(){
    	List<ZtcRouteAttachment> ZtcRouteAttachments=ztcRouteAttachmentDao.getAllZtcRouteAttachments();
    	return ZtcRouteAttachments;
    }

    public List<ZtcRouteAttachment> searchInfoEquals(ZtcRouteAttachment ztcRouteAttachment){
        List<ZtcRouteAttachment> list= ztcRouteAttachmentDao.searchInfoEquals(ztcRouteAttachment);
        return list;
    }

	/**
	 * 根据附件id或线路id删除附件
	 * @param id 附件id
	 * @param routeId 线路id
	 * @return 操作结果信息
	 */
    public String delete(String id,String routeId){
    	if(id!=null){
    		List<Attachment> attachments=attachmentManager.getAttachmentByBusinessId(id);
    		for (Attachment attachment : attachments) {
				attachmentManager.deleteAttachment(attachment);
			}
    	}
    	if(routeId!=null){
    		ZtcRouteAttachment ztcRouteAttachment=new ZtcRouteAttachment();
    		ztcRouteAttachment.setFkZtcRouteId(routeId);
    		List<ZtcRouteAttachment> ztcRouteAttachments=ztcRouteAttachmentDao.searchInfoEquals(ztcRouteAttachment);
    		for (ZtcRouteAttachment routeAttachment : ztcRouteAttachments) {
    			List<Attachment> attachments=attachmentManager.getAttachmentByBusinessId(routeAttachment.getId());
        		for (Attachment attachment : attachments) {
    				attachmentManager.deleteAttachment(attachment);
    			}
			}
    	}
        return ztcRouteAttachmentDao.delete(id,routeId);
    }

	public String update(ZtcRouteAttachment ztcRouteAttachment) {
		return ztcRouteAttachmentDao.update(ztcRouteAttachment);
	}
}
